﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Threading;
using System.IO;

namespace CA.RetailBeerSystems.POSServiceObjects.Scanners
{
	internal class NativeMethods
	{

		#region Enums

		[Flags]
		internal enum EFileAttributes : uint
		{
			Readonly = 0x00000001,
			Hidden = 0x00000002,
			System = 0x00000004,
			Directory = 0x00000010,
			Archive = 0x00000020,
			Device = 0x00000040,
			Normal = 0x00000080,
			Temporary = 0x00000100,
			SparseFile = 0x00000200,
			ReparsePoint = 0x00000400,
			Compressed = 0x00000800,
			Offline = 0x00001000,
			NotContentIndexed = 0x00002000,
			Encrypted = 0x00004000,
			Write_Through = 0x80000000,
			Overlapped = 0x40000000,
			NoBuffering = 0x20000000,
			RandomAccess = 0x10000000,
			SequentialScan = 0x08000000,
			DeleteOnClose = 0x04000000,
			BackupSemantics = 0x02000000,
			PosixSemantics = 0x01000000,
			OpenReparsePoint = 0x00200000,
			OpenNoRecall = 0x00100000,
			FirstPipeInstance = 0x00080000
		}

		#endregion

		#region P.Invoke Declarations

		[DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
		public static extern SafeFileHandle CreateFile(
			string fileName,
			[MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
			[MarshalAs(UnmanagedType.U4)] FileShare fileShare,
			IntPtr securityAttributes,
			[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
			[MarshalAs(UnmanagedType.U4)] EFileAttributes flags,
			IntPtr template);

		[DllImport("hid.dll")]
		internal static extern byte HidD_GetAttributes(SafeHandle hObject, ref _HIDD_ATTRIBUTES attributes);
		[DllImport("hid.dll", SetLastError = true)]
		internal static extern byte HidD_GetPreparsedData(SafeHandle hObject, ref IntPtr pPHIDP_PREPARSED_DATA);
		[DllImport("hid.dll", SetLastError = true)]
		internal static extern int HidP_GetCaps(IntPtr pPHIDP_PREPARSED_DATA, ref HIDP_CAPS myPHIDP_CAPS);

		#endregion

		#region Structs

		// Nested Types
		[StructLayout(LayoutKind.Sequential)]
		public struct _HIDD_ATTRIBUTES
		{
			public uint Size;
			public ushort VendorID;
			public ushort ProductID;
			public ushort VersionNumber;

		}  // END: internal struct _HIDD_ATTRIBUTES

		[StructLayout(LayoutKind.Sequential)]
		internal struct HIDP_CAPS
		{
			public ushort Usage;
			public ushort UsagePage;
			public ushort InputReportByteLength;
			public ushort OutputReportByteLength;
			public ushort FeatureReportByteLength;
			[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x11)]
			public ushort[] Reserved;
			public ushort NumberLinkCollectionNodes;
			public ushort NumberInputButtonCaps;
			public ushort NumberInputValueCaps;
			public ushort NumberInputDataIndices;
			public ushort NumberOutputButtonCaps;
			public ushort NumberOutputValueCaps;
			public ushort NumberOutputDataIndices;
			public ushort NumberFeatureButtonCaps;
			public ushort NumberFeatureValueCaps;
			public ushort NumberFeatureDataIndices;

		}  // END: internal struct HIDP_CAPS

		#endregion

		#region Constructors

		//Private constructor to prevent compiler auto-generating a public one.
		private NativeMethods()
		{
		}

		#endregion

	}  // END: internal class NativeMethods
}  // END: CA.RetailBeerSystems.POSServiceObjects.Scanners
